package hr.ctg.domain.model;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Immutable;
import org.hibernate.validator.NotNull;

@Entity
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
@Immutable
@Table(name = "drzava")
public class Drzava {
	private static final long serialVersionUID = 3164773015546853934L;

	// Hrvatska
	public static final double CRO_LONGITUDA = 16.19384765625;
	public static final double CRO_LATITUDA = 44.7779358963162;
	public static final int CRO_ZOOM = 7;

	private int idDrzave;
	private String naziv;
	private Double longituda;
	private Double latituda;
	private Integer zoom;
	private String opis;
	private String www;
	private Set<Opis> opisDrzaves = new HashSet<Opis>();

	public Drzava() {
	}

	public Drzava(final int idDrzave) {
		this.idDrzave = idDrzave;
	}

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "id_drzave", unique = true, nullable = false)
	@NotNull
	public int getIdDrzave() {
		return idDrzave;
	}

	@Column(name = "latituda", nullable = false)
	public Double getLatituda() {
		return latituda;
	}

	@Column(name = "longituda", nullable = false)
	public Double getLongituda() {
		return longituda;
	}

	@Column(name = "naziv", nullable = false)
	@NotNull
	public String getNaziv() {
		return naziv;
	}

	@Column(name = "opis")
	public String getOpis() {
		return opis;
	}

	@Column(name = "zoom", nullable = false)
	public Integer getZoom() {
		return zoom;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + idDrzave;
		return result;
	}

	@Override
	public boolean equals(final Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof Drzava)) {
			return false;
		}
		final Drzava other = (Drzava) obj;
		if (getIdDrzave() != other.getIdDrzave()) {
			return false;
		}
		return true;
	}

	public void setIdDrzave(final int idDrzave) {
		this.idDrzave = idDrzave;
	}

	public void setLatituda(final Double latituda) {
		this.latituda = latituda;
	}

	public void setLongituda(final Double longituda) {
		this.longituda = longituda;
	}

	public void setNaziv(final String naziv) {
		this.naziv = naziv;
	}

	public void setOpis(final String opis) {
		this.opis = opis;
	}

	public void setZoom(final Integer zoom) {
		this.zoom = zoom;
	}

	@Override
	public String toString() {
		return Integer.toString(idDrzave);
	}

	@Column(name = "www")
	public String getWww() {
		return www;
	}

	public void setWww(final String www) {
		this.www = www;
	}

	@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "drzava")
	public Set<Opis> getOpisDrzaves() {
		return opisDrzaves;
	}

	public void setOpisDrzaves(final Set<Opis> opisDrzaves) {
		this.opisDrzaves = opisDrzaves;
	}

	public Opis getOpis(final String currentLanguage) {
		if (opisDrzaves != null) {
			for (final Opis opisObjekta : opisDrzaves) {
				if (opisObjekta.getJezik().getOznaka().equals(currentLanguage)) {
					return opisObjekta;
				}
			}
		}
		return null;
	}
}
